import { min, max } from './index';
// 获取x轴
export const getXAxis = function (itemData) {
  let lg13 = itemData.some(e => e.data.length > 13)
  return {
    axisLine: {
      show: false,
    },
    axisTick: {
      lineStyle: {
        color: '#323233'
      }
    },
    axisLabel: {
      interval: lg13 ? 'auto' : 0
    },
    data: itemData[0].data.map(x => {
      if (x.x.indexOf('-') != -1) {
        let formatDate = x.x.split('-')
        return {
          value: `${Number(formatDate[1])}.${Number(formatDate[2])}`,
          textStyle: {
            color: '#fff'
          }
        }
      } else {
        return isNaN(Number(x.x)) ?
          { value: x.x, textStyle: { color: '#fff' } } :
          { value: x.x + '月', textStyle: { color: '#fff' } }
      }
    }),
  }
}

// 获取y轴
export const getYAxis = function (itemData) {
  let leftArr = [] //左轴数据
  let rightArr = [] //右轴数据
  let unitList = [] //单位 ['转变率:万', '同比:%'] 双 || ['万'] 单
  let notNeeY = itemData.find(e => !e.needY).data[0]

  let tName = notNeeY.targetName
  unitList.push(tName + ':' + notNeeY.unit)
  itemData.filter(e => !e.needY).forEach(e => {
    e.data.forEach(el => {
      el.targetValue != null && leftArr.push(el.targetValue)
    })
  })
  let needY = itemData.find(e => e.needY)
  if (needY) {   //需要双y轴
    let ntName = needY.data[0].targetName
    ntName = ntName.replace('正', '')
    ntName = ntName.replace('负', '')
    unitList.push(ntName + ':' + needY.data[0].unit)
    itemData.filter(e => e.needY == true).forEach(e => {
      e.data.forEach(el => {
        el.targetValue != null && rightArr.push(el.targetValue)
      })
    })
  }

  return unitList.map((unit, index) => ({
    name: unitList.length > 1 ? `${unit}` : `单位:${unit.split(':')[1]}`,
    position: index == 1 ? 'right' : 'left',
    min: index == 0 ? min(Math.min(...leftArr)) : min(Math.min(...rightArr)),
    max: index == 0 ? max(Math.max(...leftArr)) : max(Math.max(...rightArr)),
    axisLine: {
      show: true,
    },
    //网格线
    splitLine: {
      show: true,
      lineStyle: {
        color: '#ffffff',
        opacity: 0.1,
      },
    },
    axisLabel: {
      textStyle: {
        color: '#fff',
      },
    },
    nameTextStyle: {
      color: '#fff',
    },
  }))
}
